//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    motto: 'Hello World',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    name :'Tom',
    num:'123456789',
    shareData: {
      title: '你好，我是Tom，这是我的微信名片，请您惠存',
      desc: '自定义分享描述',
      path: '/pages/share/share?name'
    }
  },
  onLoad() {
    this.ctx = wx.createCameraContext()
  },
  onShareAppMessage: function () {
    //return this.data.shareData
    var url = '/pages/share/share?name='+this.data.name
    console.log(url);
    return {
      title: '你好，我是Tom，这是我的微信名片，请您惠存',
      desc: '自定义分享描述',
      path: url
    }
  },
  // takePhoto() {
  //   this.ctx.takePhoto({
  //     quality: 'high',
  //     success: (res) => {
  //       // this.setData({
  //       //   src: res.tempImagePath
  //       // })
  //     //   wx.saveImageToPhotosAlbum({
  //     //     filePath: res.tempImagePath,
  //     //     success: function (res) {
  //     //       console.log(res)
  //     //     },
  //     //     fail: function (res) {
  //     //       console.log(res)
  //     //       console.log('fail')
  //     //     }
  //     // })
  //   })
  // },
  takePhoto() {
    this.ctx = wx.createCameraContext();
    this.ctx.takePhoto({
      quality: 'high',
      success: (res) => {
        this.setData({
          src: res.tempImagePath
        })
        console.log(res)
        //
        wx.saveImageToPhotosAlbum({
          filePath: res.tempImagePath,
          success: function (res) {
            console.log(res)
          },
          fail: function (res) {
            console.log(res)
            console.log('fail')
          }
          })
        //
      }
    })
  },
  // 长按号码响应函数
  phoneNumTap: function () {
    var that = this;
    console.log(that)
    // 提示呼叫号码还是将号码添加到手机通讯录
    wx.showActionSheet({
      itemList: ['呼叫', '添加联系人'],
      success: function (res) {
        if (res.tapIndex == 0) {
          // 呼叫号码
          wx.makePhoneCall({
            phoneNumber: that.data.num,
          })
        } else if (res.tapIndex == 1) {
          // 添加到手机通讯录
          wx.addPhoneContact({
            firstName: that.data.name,//联系人姓名
            mobilePhoneNumber: that.data.num,//联系人手机号
          })
        }
      }
    })
  },
  bindKeyInputName: function (e) {
    this.data.name = e.detail.value;
    console.log(this.data);
  },
  bindKeyInputNum: function (e) {
    this.data.num = e.detail.value;
    console.log(this.data);
  },
  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onLoad: function () {
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse){
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }
  },
  getUserInfo: function(e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  }
})
